package chapter14;

import java.util.*;
import java.math.*;

public class Exercise14 {

	public static void main(String[] args) throws CloneNotSupportedException {
		
		//exerciseProgrammer14_1();
		//exerciseProgrammer14_3();
		//exerciseProgrammer14_2();
		//exerciseProgrammer14_4();
		//exerciseProgrammer14_5();
		//exerciseProgrammer14_8();
//		exerciseProgrammer14_11();
		boolean a = new Integer(42) == new Integer(42);
		System.out.println(a);
	}
	
	public static void exerciseProgrammer14_11(){
		String initString = String.valueOf(Long.MAX_VALUE);
		BigInteger initValue = new BigInteger(initString);
		
		System.out.println("initValue is " + initValue);
		
		BigInteger five = new BigInteger("5");
		BigInteger six = new BigInteger("6");
		BigInteger one = new BigInteger("1");
		BigInteger zero = new BigInteger("0");
		BigInteger loopVar = initValue.add(one);
		int i = 0;
		
		while (i < 10){
			if (loopVar.remainder(five).equals(zero) || loopVar.remainder(six).equals(zero)){
				i++;
				System.out.println(i + " : " + loopVar);
			}
			loopVar = loopVar.add(one);
		}
		
		
	}
	
	public static void exerciseProgrammer14_8(){
		GeometricObject[] geoArr = new GeometricObject[4];
		geoArr[0] = new Circle(3);
		geoArr[1] = new Circle(7);
		geoArr[2] = new Rectangle(3,4);
		geoArr[3] = new Rectangle(6,9);
		double sumOfGeometricObject = sumArea(geoArr);
		System.out.println(sumOfGeometricObject);
	}
	
	public static double sumArea(GeometricObject[] a){
		/*编程题14.8*/
		double sum = 0;
		for (int i =0; i < a.length; i++){
			sum += a[i].getArea();
		}
		return sum;
	}
	
	public static void exerciseProgrammer14_5(){
		ComparableCircle c1 = new ComparableCircle(2.3);
		ComparableCircle c2 = new ComparableCircle(5.3);
		ComparableCircle c3 = new ComparableCircle(5.3);
		System.out.println(c2.equals(c1));
		System.out.println(c2.equals(c3));
	}
	
	public static void exerciseProgrammer14_4() throws CloneNotSupportedException{
		Circle c1 = new Circle(2.2);
		Circle c1Cloned = (Circle) ((GeometricObject)c1).clone();
		System.out.println(c1 == c1Cloned);
		System.out.println(c1.getDateCreate() == c1Cloned.getDateCreate());
		System.out.println(c1.getDateCreate().compareTo(c1Cloned.getDateCreate()));
		System.out.println(c1.compareTo(c1Cloned));
		
	}
	
	public static void exerciseProgrammer14_2(){
		ComparableCircle c1 = new ComparableCircle(2.3);
		ComparableCircle c2 = new ComparableCircle(5.3);
		ComparableCircle cMax = ComparableCircle.max(c1, c2);
		System.out.println("The max of c1 and c2 is " + (cMax == c1 ? "c1" : "c2"));
	}
	
	public static void exerciseProgrammer14_3(){
		GeometricObject[] geoArr = new GeometricObject[5];
		geoArr[0] = new Rectangle(2.4, 2.5);
		geoArr[1] = new Circle(3);
		geoArr[2] = new Square();
		geoArr[3] = new Square(2.0);
		geoArr[4] = new Rectangle();
		
		for (int i = 0; i < geoArr.length; i++){
			if (geoArr[i] instanceof Colorable){
				//geoArr[i].howToColor();-->这种写法是错误的，因为GeometricObject并没有实现howToColor
				((Colorable)geoArr[i]).howToColor();
			}
		}
	}
	
	public static void exerciseProgrammer14_1(){
		 Circle c1 = new Circle(2.5);
		 Circle c2 = new Circle(4);
		 System.out.println(GeometricObject.max(c1, c2 ));
		 
		 Rectangle r1 = new Rectangle(3,4);
		 Rectangle r2 = new Rectangle(5,6);
		 System.out.println(GeometricObject.max(r1, r2));
		 
		 System.out.println(GeometricObject.max(r1, c2));
		 
	}
	
	public static void exercise14_24(){
		//Object[] i = new int[10];
		Object[] s = new String[10];
		Object[] o = new Object[50];
		Object[] c = new Calendar[20];

	}
	
	public static void exercise14_20(){
		Number x = new Integer(3);
		System.out.println(x.intValue());
		System.out.println(((Integer)x).compareTo(new Integer(4)));
	} 
	
	public static void exercise14_18(){
		Number numberRef = new Integer(0);
		//Integer intRef = (Integer) numberRef;
		Double doubleRef = (Double) numberRef;//java.lang.ClassCastException
	} 
	
	public static void exercise14_16(){
		int i = (Double.valueOf("23.4")).intValue();
		String s = (Double.valueOf("23.4")).toString(); 
		System.out.println(i);
		System.out.println(s);
	} 
	                                                                             
	public static void exercise14_13() throws CloneNotSupportedException {
		GeometricObject x = new Circle(3); 
		GeometricObject y = (GeometricObject) x.clone();
		System.out.println(x == y);
	}

}

   